﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Servers
{
    public class LeaveServers
    {
        private readonly IRepository<attend_leave> attend_leaveRepository;

        public LeaveServers()
        {
            attend_leaveRepository = new DapperRepository<attend_leave>();

        }

        public List<attend_leave> GetData()
        {
            var datas = attend_leaveRepository.GetAllEnumerable();

            return datas.ToList();
        }

        public attend_leave GetModelById(string id)
        {
            return attend_leaveRepository.GetById(id);
        }


        internal void Add(attend_leave model)
        {
            attend_leaveRepository.Insert(model);
        }

        internal void Update(attend_leave model)
        {
            attend_leaveRepository.Update(model);
        }
        
        internal void UpdateStatus(int id, int status, string reason)
        {
            string sql = string.Format("UPDATE [dbo].[attend_leave] SET [ALLOW] = '{0}',[REASON]='{1}',[HANDLE_TIME]=GETDATE() WHERE [LEAVE_ID] = {2}", status, reason, id);
            attend_leaveRepository.SqlWithParams(sql);
        }

        internal void Del(string id)
        {
            attend_leaveRepository.DeleteByIds(id);
        }


        internal List<SelectListItem> GetSTATUS()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = attend_leaveRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetALLOW()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = attend_leaveRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal List<SelectListItem> GetLEAVE_TYPE2()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            //var dstable = attend_leaveRepository.SqlWithParamsDynamic("select a.name Value,concat(a.name,'-----',convert(nvarchar(max),b.value)) Text from sys.tables  a inner join  sys.extended_properties b on a.object_id=b.major_id and b.minor_id=0");
            var dstable = attend_leaveRepository.SqlWithParamsDynamic("SELECT [LEAVE_TYPE_NAME] AS 'Value',[LEAVE_TYPE_NAME] AS 'Text' FROM [OADB].[dbo].[attend_leave_type]");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }


    }
}